'use strict';

const Controller = require('egg').Controller;
const jwt = require("jsonwebtoken")
const mysql = require("mysql");
class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  //科普热点渲染
  async hotdata() {
    const { ctx, app } = this;
    let data = await app.mysql.select('sciencelist')
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  //登录
  async login() {
    const { ctx, app } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.get('logins', { username, password })
    const token = jwt.sign(username, 'lm')
    if (data) {
      ctx.body = {
        code: 1,
        token,
        data,
        msg: '登录成功'
      }
    } else {
      ctx.body = {
        code: 0,
        msg: '登录失败'
      }
    }
  }

  //注册
  async register() {
    const { ctx, app } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.get('logins', { username })
    if (data) {
      ctx.body = {
        code: 2,
        msg: '账户已存在'
      }
    } else {
      await app.mysql.insert('logins', { username, password })
      ctx.body = {
        code: 3,
        msg: '注册成功'
      }
    }
  }
  //搜索
  async searchshop() {
    const { ctx, app } = this;
    const { value } = ctx.request.body
    let data = await app.mysql.query(`select * from sciencelist where title like "%${value}%"`)
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
   //首页渲染
   async homelist() {
    const { ctx, app } = this;
    let data = await app.mysql.select('homelist')
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  //商城渲染
  async shop() {
    const { ctx, app } = this;
    let data = await app.mysql.select('shoplist')
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  //购物车
  async shopcarlist() {
    const { ctx, app } = this;
    const data = await app.mysql.select('shopcar')
    ctx.body = {
      code: '000',
      data
    }
  }
  //添加到购物车
  async addcar() {
    const { ctx, app } = this;
    const { id, title, img, price, num ,type} = ctx.request.body;
    const data = await app.mysql.get('shopcar', { id, title, img, price ,type})
    //判断是否有该数据
    if (data) {
      //如果有数量+1
      await app.mysql.update('shopcar', { num: data.num + 1, id })
    } else {
      //如果没有添加一条
      await app.mysql.insert('shopcar', { num: num + 1, title, img, price, id, flag: 0 ,type})
    }
    ctx.body = {
      code: '000',
      msg: '请求成功'
    }
  }

  async delcar() {
    const { ctx, app } = this
    const { id } = ctx.request.body;
    const data = await app.mysql.delete('shopcar', { id })
    ctx.body = {
      data,
      code: '000'
    }
  }
  async changecar() {
    const { ctx, app } = this
    const { value, id } = ctx.request.body;
    console.log(value);
    const data = await app.mysql.update('shopcar', { num: value, id })
    ctx.body = {
      data,
      code: '000'
    }
  }

  async exitflag() {
    const { ctx, app } = this
    const { id, flag } = ctx.request.body;
    console.log(!!flag);

    if (flag === 0) {
      await app.mysql.update('shopcar', { id, flag: 1 })
      ctx.body = {
        data: await app.mysql.select('shopcar', { id }),
        code: '000'
      }
    } else if (flag === 1) {
      const data = await app.mysql.update('shopcar', { id, flag: 0 })
      ctx.body = {
        data,
        code: '001'
      }
      console.log(data);
    }
  }
  async checkall() {
    const { ctx, app } = this
    await app.mysql.UPDATE(`shopcar SET flag = '1'`)
    // await app.mysql.query(`UPDATE shopcar  SET flag = '1' `)
  }
  //话题
  async title() {
    const { ctx, app } = this;
    let data = await app.mysql.select('titlelist')
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  async shopcar() {
    const { ctx, app } = this;
    let data = await app.mysql.select('shopcar')
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  } 
  //搜索
  async searchtype() {
    const { ctx, app } = this;
    const { value } = ctx.request.body
    let data = await app.mysql.query(`select * from shopcar where type like "%${value}%"`)
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
}

module.exports = HomeController;
